package cn.shop.dys.util;

import android.content.Context;
import android.os.Handler;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import cn.shop.dys.R;
import cn.shop.dys.bean.CategoryItem;


public class CategoryViewPager extends LinearLayout implements OnClickListener, OnPageChangeListener {

	private ViewPager vp;
	private LinearLayout ll_points;
	private List<CategoryItem> items;
	private TextView tv_title;

	public CategoryViewPager(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}
	

	private void init() {
		View.inflate(getContext(), R.layout.category_vpp_mian, this);
		initView();
	}

	private void initView() {
		vp = (ViewPager) findViewById(R.id.vp);
		tv_title = (TextView) findViewById(R.id.tv_title);
		ll_points = (LinearLayout) findViewById(R.id.ll_points);
		
	}
	
	public void setItems(List<CategoryItem> items){
		this.items = items;
		for (int i = 0; i <items.size(); i++) {
			View point = new View(getContext());
			point.setBackgroundResource(R.drawable.point_selector);
			LayoutParams params = new LayoutParams(10,10);
			params.leftMargin = 15;
			
			point.setLayoutParams(params);
			point.setOnClickListener(this);
			
			point.setTag(i);
			ll_points.addView(point);
		}
		
		vp.setAdapter(new MyAdapter());
		vp.setCurrentItem(1);
		tv_title.setText(items.get(0).getTitle());
		ll_points.getChildAt(0).setEnabled(false);
		
	}
	
	private Handler handler = new Handler();
	
	// 在自定义控件绑定窗体,创建自身的时候回调的方法
	@Override
	protected void onAttachedToWindow() {
		super.onAttachedToWindow();
		// 为ViewPager添加页面改变的监听
		vp.addOnPageChangeListener(this);
		handler.postDelayed(new MyRunnable(), 1000);
	}

	private boolean isStop=false;
	class MyRunnable implements Runnable{

		@Override
		public void run() {
			if (!isStop) {
				//Log.i("test", "自动滚动");
				vp.setCurrentItem(vp.getCurrentItem()+1);
				handler.postDelayed(this, 1000);
			}
		}
		
		
	}
	
	// 在自定义控件销毁前的回调方法
	@Override
	protected void onDetachedFromWindow() {
		super.onDetachedFromWindow();
		// 移除ViewPager监听
		vp.removeOnPageChangeListener(this);
		isStop = true;
	}
	
	class MyAdapter extends PagerAdapter {

		@Override
		public int getCount() {
			
			return items.size()+2;
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			
			return arg0 == arg1;
		}
		
		public List<ImageView> cachas = new ArrayList<ImageView>();
		
		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			Log.i("test", "instantiateItem----------");
			if (cachas.isEmpty()) {
				ImageView iv = new ImageView(getContext());
				iv.setScaleType(ScaleType.FIT_XY);
				cachas.add(iv);
				
			}
			
			ImageView iv = cachas.remove(0);
			if (position == 0) {
				Categorylmageloader.download(getContext(), items.get(items.size()-1).getUrl(), iv);
				
			}else if (position == items.size() + 1) {
				Categorylmageloader.download(getContext(), items.get(0).getUrl(), iv);
			}else {
				Categorylmageloader.download(getContext(), items.get(position-1).getUrl(), iv);
			}
			
			container.addView(iv);
			return iv;
		}
		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			ImageView iv = (ImageView) object;
			container.removeView(iv);
			iv.setImageBitmap(null);
			cachas.add(iv);
			Log.i("test", "caches:" + cachas.size());
			
		}
	}
	
	// 滚动状态改变的时候
	@Override
	public void onPageScrollStateChanged(int state) {
		if (OnScrollListener.SCROLL_STATE_IDLE == state) {
			if (position == 0 || position == items.size()+1) {
				// 让ViewPager跳转到对应的pos上
				vp.setCurrentItem(pos, false);
			}
		}
		
		
	}
	
	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
		
		
	}
	private int prePosition = 1;
	private int pos;
	private int position;
	@Override
	public void onPageSelected(int position) {
		this.position = position;
		this.pos = position;
		if (position == 0) {
			pos = items.size();
		}else if (position == items.size()+1) {
			pos = 1;
		}
		if (prePosition != pos) {
			Log.i("test", "pos:" + pos + ", prePosition:" + prePosition);
			tv_title.setText(items.get(pos - 1).getTitle());
			
			ll_points.getChildAt(pos-1).setEnabled(false);
			ll_points.getChildAt(prePosition - 1).setEnabled(true);
			
			prePosition = pos;
		}
	}
	
	@Override
	public void onClick(View v) {
		int i = (Integer) v.getTag();
		vp.setCurrentItem(i+1);
		
	}



}
